package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.bean.MyAddressBean;
import com.dingwei.superiordiary.mvp.presenter.AddAddressPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.AppManager;
import com.lihao.baselibrary.utils.L;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 南柯一梦丶 on 2018/1/11.
 *
 * 新增收货地址
 */

public class AddAddressActivity extends BaseActivity implements Contract.AddAddressView {
    AddAddressPresenter addAddressPresenter = new AddAddressPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.tv_city)
    TextView tvCity;
    @Bind(R.id.ll_check_city)
    LinearLayout llCheckCity;
    @Bind(R.id.et_details_address)
    EditText etDetailsAddress;
    @Bind(R.id.btn_keep)
    SuperButton btnKeep;

    MyAddressBean.DataBean bean ;

    boolean isAdd = false;

    private String provinceId;
    private String cityId;
    private String countyId;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initData() {

    }

    protected void initView() {
        if (getIntent().hasExtra("bean")){
            tvTitle.setText("编辑收货地址");
            bean = (MyAddressBean.DataBean) getIntent().getSerializableExtra("bean");
            provinceId = bean.province_id;
            cityId = bean.city_id;
            countyId = bean.county_id;
            etName.setText(bean.name);
            etPhone.setText(bean.mobile);
            tvCity.setText(bean.province+bean.city+bean.county);
            etDetailsAddress.setText(bean.address);
            isAdd = false;
        }else {
            isAdd = true;
            tvTitle.setText("新增收货地址");
        }
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.img_back, R.id.ll_check_city, R.id.btn_keep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_check_city:
                addAddressPresenter.getProvince();
                break;
            case R.id.btn_keep:
                if (isAdd){
                    addAddressPresenter.keep(etName.getText().toString().trim(),etPhone.getText().toString().trim(),provinceId,cityId,countyId,etDetailsAddress.getText().toString(),"");
                }else {
                    addAddressPresenter.keep(etName.getText().toString().trim(),etPhone.getText().toString().trim(),provinceId,cityId,countyId,etDetailsAddress.getText().toString(),bean.id);
                }
                break;
        }
    }

    @Override
    public void checkCity(String provinceId, String provinceName, String cityId, String cityName, String countyId, String countyName) {
        this.provinceId = provinceId;
        this.cityId = cityId;
        this.countyId = countyId;
        tvCity.setText(provinceName + cityName + countyName);
    }

    @Override
    public void saveAddressSuccess() {
         AppManager.getAppManager().finishActivity();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
